package cn.lzm.server.impl;

import cn.lzm.AjaxResult;
import cn.lzm.domain.Product;
import cn.lzm.domain.Store;
import cn.lzm.feign.StoreFeignClient;
import cn.lzm.mapper.ProductMapper;
import cn.lzm.mapper.StoreMapper;
import cn.lzm.server.IProductService;
import cn.lzm.server.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreFeignClient storeFeignClient;
    @Override
    public Product selectOne(String name) {
        return null;
    }

    @Override
    public List<Product> selectAll() {
        return productMapper.selectAll();
    }

    @Override
    public AjaxResult addOne(Product product) {
        Product product1 = productMapper.selectOne(product.getName());
       

            productMapper.addOne(product);
            Store store = new Store();
            store.setProductId(product.getId());
            store.setNumber(product.getNumber());
            AjaxResult ajaxResult = storeFeignClient.addOne(store);
            return ajaxResult;


//        if (product1 == null) {
//            productMapper.addOne(product);
//            Store store = storeMapper.selectByProductId(product.getId());
//            store.setNumber(product.getNumber());
//            storeMapper.addOne(store);
//        }else {
//            productMapper.updateOne(product);
//            Product product2 = productMapper.selectOne(product.getName());
//            Store store = storeMapper.selectByProductId(product2.getId());
//            Integer number = store.getNumber()+product2.getNumber();
//            store.setNumber(number);
//            storeMapper.updateOne(store);
//        }

    }

    @Override
    public void updateOne(Product product) {

    }
}
